<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
namespace app\backend\logic;
use \think\facade\Session;
use \tpfcore\Core;
/**
 *  管理员逻辑
 */
class Admin extends AdminBase
{
	public function logout(){
		Session::delete("backend_author_sign");
		return [RESULT_SUCCESS, '注销成功', url('Admin/login')];
	}
	public function login($data){
		$admin_login_limit_ip=config("config.ADMIN_LOGIN_LLIMIT_IP");
		//如果设置了IP限制登录
		if(!empty($admin_login_limit_ip)){
			$request_ip=request()->ip();
			if(stripos($request_ip,"127.0.0.")===false && !in_array($request_ip, explode(",", $admin_login_limit_ip))){
				return [RESULT_ERROR,'该IP禁止登录',url('Admin/login')];
			}
		}
		$scene=config('config.ADMIN_LOGIN_VERIFY_SWITCH')?"select":"no_captcha";
		$validate=Core::loadValidate($this->name);
		$validate_result = $validate->scene($scene)->check($data);
        if (!$validate_result) {    
            return [RESULT_ERROR, $validate->getError(), null];
        }

        $user=self::getOneObject(["username"=>$data['username'],"password"=>'###'.md5($data['password'].DATA_ENCRYPT_KEY),'status'=>0]);
        if(empty($user)){
        	return [RESULT_ERROR, '用户名或密码错误', url('Admin/login')];
        }
        self::saveObject(['last_login_time'=>time(),"id"=>$user['id']]);
        Session::set("backend_author_sign",array("username"=>$data['username'],"userid"=>$user['id']));
		return [RESULT_SUCCESS, '登录成功', url('Admin/login')];
	}
	public function getAdminList($where = [], $field = true, $order = '', $is_paginate = true){
		$paginate_data = $is_paginate ? ['rows' => DB_LIST_ROWS] : false;
		return self::getObject($where, $field, $order, $paginate_data);
	}
	public function uppwd($data){
		$validate=Core::loadValidate("Admin");
		$validate_result = $validate->scene('edit')->check($data);
        if (!$validate_result) {    
            return [RESULT_ERROR, $validate->getError(), null];
        }
        if(!$this->checkPassword($data)){
        	return [RESULT_ERROR, '旧密码不正确', null];
        }
        if(self::updateObject(['username'=>Session::get("backend_author_sign")['username']],["password"=>"###".md5($data['password'].DATA_ENCRYPT_KEY)])){
        	Session::delete("backend_author_sign");
        	return [RESULT_SUCCESS, '修改密码成功，请重新登录', url('Admin/login')];
        } 
	}
	public function upAdminInfo($data){
		$validate=Core::loadValidate("Admin");
		$validate_result = $validate->scene('upinfo')->check($data);
        if (!$validate_result) {    
            return [RESULT_ERROR, $validate->getError(), null];
        }
        $result=self::updateObject(['id'=>Session::get("backend_author_sign")['userid']],$data);
        if($result){
        	return [RESULT_SUCCESS, '修改资料成功', null];
        }else{
        	return [RESULT_ERROR, '操作失败', null];
        }
	}
	public function getInfo($where){
		return Core::loadModel("Admin")->getOneObject($where);
	}
	public function checkPassword($data){
		return self::getStatistics(['username'=>Session::get("backend_author_sign")['username'],"password"=>"###".md5($data['old_password'].DATA_ENCRYPT_KEY)]);
	}
	public function addAdmin($data){
		$validate=Core::loadValidate($this->name);
		$validate_result = $validate->scene('add')->check($data);
        if (!$validate_result) {    
            return [RESULT_ERROR, $validate->getError(), null];
        }
        $result=Core::loadModel($this->name)->saveObject($data);
		if($result){
			$data['role_id'] && self::updateObject(['username'=>$data['username']],['privs'=>Core::loadModel("Role")->getColumnValue(["id"=>input("role_id")],"privs")]);
			return [RESULT_SUCCESS, '操作成功', url('Admin/index')];
		}else{
			return [RESULT_ERROR, '操作失败', url('Admin/index')];
		}
	}
	public function editAdmin($data){
		$validate=Core::loadValidate("Admin");
		$validate_result = $validate->scene('edit')->check($data);
        if (!$validate_result) {    
            return [RESULT_ERROR, $validate->getError(), null];
        }
        $result=Core::loadModel($this->name)->saveObject($data);
		if($result){
			$data['role_id'] && self::updateObject(['id'=>$data['id']],['privs'=>Core::loadModel("Role")->getColumnValue(["id"=>input("role_id")],"privs")]);
			return [RESULT_SUCCESS, '操作成功', url('Admin/index')];
		}else{
			return [RESULT_ERROR, '操作失败', url('Admin/index')];
		}
	}
	public function delAdmin($data){
		return self::deleteObject("id={$data['id']} and id<>1",true)?[RESULT_SUCCESS,"操作成功",url("Admin/index")]:[RESULT_ERROR,'操作失败',url('Admin/index')];
	}
	
	public function priv($data){
		$result=Core::loadModel($this->name)->saveObject($data);
		if($result){
			self::updateObject(["id"=>$data['id']],['role_id'=>0]);
			return [RESULT_SUCCESS, '操作成功', url('Admin/index')];
		}
		return [RESULT_ERROR, '操作失败', url('Admin/index')];
	}
}